package top.laoshuzi.designpatterns.base.created.prototype.kotlin

import org.junit.Test

/**
 * 客户端
 */
class Client {

    @Test
    fun operation() {

        //初始化原型1原型2
        val p1 = ConcretePrototype1("this is ConcretePrototype1")
        val p2 = ConcretePrototype2("this is ConcretePrototype2")

        //注册到原型管理器
        PrototypeManager.registerPrototype("Prototype1", p1)
        PrototypeManager.registerPrototype("Prototype2", p2)

        //获取原型克隆
        try {
            val prototype1 = PrototypeManager.getPrototype("Prototype1")
            val prototype2 = PrototypeManager.getPrototype("Prototype2")

            println("prototype1:" + prototype1.getName())
            println("p1:" + p1.getName())
            println("prototype1:" + System.identityHashCode(prototype1))
            println("p1:" + System.identityHashCode(p1))
            println("prototype2:" + prototype2.getName())
            println("p2:" + p2.getName())
            println("prototype2:" + System.identityHashCode(prototype2))
            println("p2:" + System.identityHashCode(p2))
        } catch (e: Exception) {
            e.printStackTrace()
        }

        //从原型管理器注销
        PrototypeManager.unregisterPrototype("Prototype1")
        PrototypeManager.unregisterPrototype("Prototype2")
    }
}